package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: Sabrina
 * @Date:2025/06/15/10:19
 * @Description:
 */
@Mapper
public interface OrderMapper {
    /**
     * 插入一条数据到订单表
     * @param order
     */
    void insert(Orders order);

    /**
     * 插入n条数据到订单明细表
     * @param orderDetails
     */
    void insertBatch(List<OrderDetail> orderDetails);

    /**
     * 查询订单详情(查一条)
     * @param orderId
     * @return
     */
    @Select("SELECT * FROM orders where id = #{orderId}")
    Orders getOrderById(Integer orderId);

    /**
     * 查看订单详情(查多条且分页)
     * @param userId
     * @param status
     * @return
     */
    Page<Orders> getOrders(Long userId, Integer status);

    /**
     * 根据订单号和用户id查询订单
     * @param orderNumber
     * @param userId
     */
    @Select("select * from orders where number = #{orderNumber} and user_id= #{userId}")
    Orders getByNumberAndUserId(String orderNumber, Long userId);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);


    /**
     * 取消订单-修改这一条订单数据
     * @param orderId
     */
    @Delete("UPDATE orders set status = 6 where id = #{orderId}")
    void cancel(Integer orderId);

    @Select("SELECT * FROM order_detail WHERE order_id = #{orderId}")
    List<OrderDetail> getOrderDetail(Long orderId);

    /**
     * 删除当前用户此订单明细
     * @param orderId
     */
    @Delete("DELETE FROM order_detail where order_id = #{orderId}")
    void deleteBatches(Integer orderId);


    @Select("SELECT * FROM orders WHERE status = 1 AND order_time < #{time}")
    List<Orders> getOrdersByOrderTime(LocalDateTime time);

    @Select("SELECT * FROM orders WHERE status = 4 AND order_time < #{time}")
    List<Orders> getOrdersByOrderTime2(LocalDateTime time);
}
